package com.example.noelwhereismystuffs.views.ReportItemsActivity;

import java.util.ArrayList;
import java.util.List;

import com.example.noelproject.R;
import com.example.noelwhereismystuffs.models.Database;
import com.example.noelwhereismystuffs.models.Item;
import com.example.noelwhereismystuffs.models.ReportItemsModel;
import com.example.noelwhereismystuffs.models.User;
import com.example.noelwhereismystuffs.presenters.ReportItemsPresenter;
import com.example.noelwhereismystuffs.views.DisplayLostItemsActivity.DisplayLostItemsActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.app.Activity;
import android.content.Intent;

public class ReportItemsActivity extends Activity implements IReportItemsView {
	private ReportItemsPresenter myPresenter;
	@SuppressWarnings("unused")
	private ReportItemsModel myModel;
	private User myUser = User.NULL_USER;
	private Database myData;
	private RadioGroup itemsRadioButtonGroup;
	private RadioButton itemsRadioButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Retrieve the user from UserProfile Activity
		myUser = (User) getIntent().getSerializableExtra("User");
		myData = (Database) getIntent().getSerializableExtra("Database");
		// Create presenter
		myPresenter = new ReportItemsPresenter(this, new ReportItemsModel(myData));
		setContentView(R.layout.activity_report_items);
	}

	/**
	 * Handle Report button
	 * 
	 * @param source
	 *            from xml file
	 */
	public void onReportClick(View source) {
		itemsRadioButtonGroup = (RadioGroup) findViewById(R.id.ItemTypesRadioButtonGroup);
		// get selected radio button from radioGroup
		int selectedId = itemsRadioButtonGroup.getCheckedRadioButtonId();

		// find the radiobutton by returned id
		itemsRadioButton = (RadioButton) findViewById(selectedId);
		String itemType = itemsRadioButton.getText().toString();
		// get the item's info that entered from user
		String itemName = getItemNameField().getText().toString();
		String itemLocation = getItemLocationField().getText().toString();
		String itemDate = getItemDateField().getText().toString();
		String categoryField = getCategoryField().getSelectedItem().toString();
		// give the prenter data that has been entered
		myPresenter.addNewItem(myUser, itemType, itemName, itemLocation, categoryField, itemDate);
	}

	/**
	 * Handle Cancel button
	 * 
	 * @param source
	 *            from the xml file
	 */
	public void onCancelClick(View source) {
		advanceCancel();
	}

	/**
	 * Get information from Name Field
	 * 
	 * @return
	 */
	private EditText getItemNameField() {
		return (EditText) findViewById(R.id.ItemName);
	}

	/**
	 * Get information from Locaion Field
	 * 
	 * @return
	 */

	private EditText getItemLocationField() {
		return (EditText) findViewById(R.id.ItemLocation);
	}
	
	/**
	 * @return category field
	 */
	private Spinner getCategoryField() {
		return (Spinner)findViewById(R.id.categoriesSpinnerReportItems);
	}
	
	/**
	 * @return date field
	 */
	private EditText getItemDateField(){
		return (EditText) findViewById(R.id.ItemDate);
	}
	
	@Override
	public void advanceCancel() {
		// create an intent (a request for phone OS to do something)
		Intent intent = new Intent(this, DisplayLostItemsActivity.class);
		// pass the user
		intent.putExtra("User", myUser);
		intent.putExtra("Database", myData);
		// now pass that to the phone OS and ask to start it
		startActivity(intent);
	}

	@Override
	public void advanceReport(ArrayList<Item> itemList) {
		// create an intent (a request for phone OS to do something)
		Intent intent = new Intent(this, DisplayLostItemsActivity.class);
		// pass the user
		intent.putExtra("User", myUser);
		intent.putExtra("Database", myData);
		intent.putExtra("itemList", itemList);
//		intent.putExtra("itemList", itemList);
		// now pass that to the phone OS and ask to start it
		startActivity(intent);
	}

}
